package pers.cz.controller;

import pers.cz.pub.RespBean;
import pers.cz.redis.RedisQueueProductor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 开始执行testcase
 * @program: PostGirl-panent
 * @description: StartRunTestCase
 * @author: Cheng Zhi
 * @create: 2021-03-13 16:27
 **/

@RestController
@Api(tags = "启动测试案例")
@RequestMapping("/api/runTestCase")
public class StartRunTestCase {

    @Autowired
    RedisQueueProductor redisQueueProductor;

/*    @ApiOperation(value = "通知启动测试案例")
    @PostMapping("/startRun")
    public RespBean startRun(@RequestBody List<Integer> ids) {

        StringBuilder stringIds = new StringBuilder();
        for (Integer id : ids) {
            stringIds.append(id);
        }
        RequestQueue.addQueue(ids);
        redisQueueProductor.putMessageForTestCase(stringIds.toString());

        return RespBean.ok("启动消息发送成功,测试结果请稍后查询");
    }*/

    @ApiOperation(value = "通知启动自动化测试案例")
    @PostMapping("/startRunAutoTestCase")
    public RespBean startRunAutoTestCase() {

        return RespBean.ok("启动成功");
    }

    @ApiOperation(value = "通知启动测试案例")
    @PostMapping("/startRun")
    public RespBean startRun(@RequestBody Map map) {

        String hostAddress = (String) map.get("hostAddress");
        List<Integer> ids = (List<Integer>) map.get("ids");
        String type = (String) map.get("type");
        String defaultDataBase = (String) map.get("defaultDataBase");
        StringBuilder stringIds = new StringBuilder();
        for (Integer id : ids) {
            stringIds.append(id);
            stringIds.append(",");
        }
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String value = stringIds.substring(0,stringIds.length()-1) + "^" + hostAddress + "^" + userId + "^" + type + "^" + defaultDataBase;
        redisQueueProductor.putMessageForTestCase(value);

        return RespBean.ok("启动消息发送成功,测试结果请稍后查询");
    }
}
